App({
  data: {
    user: "",
    nickName: "",
    avatarUrl: "",
    gender: "",
    province: "",
    city: "",
    country: ""
  },
  onLaunch: function () {
    var that = this;
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 获取用户信息
    wx.getSetting({//判断是否授权
      success: res => {
        if (res.authSetting["scope.userInfo"]) {
          // console.log(res.authSetting['scope.userInfo'])
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框

          // 登录
          wx.login({
            success: res => {
              // 发送 res.code 到后台换取 openId, sessionKey, unionId
              if (res.code) {
                //加载框开始
                wx.showLoading({
                  title: '加载中',
                });
                //发起网络请求
                wx.request({
                  url: 'https://jgj.hzlingdian.com/coreUser/get/openid',
                  data: {
                    code: res.code
                  },
                  success: function (res1) {
                    // console.log(this);
                    var openidUser = res1.data.data;
                    //获取用户信息

                    wx.getUserInfo({
                      success: res2 => {
                        // 可以将 res2 发送给后台解码出 unionId
                        // console.log(res2)
                        that.globalData.userInfo = res2.userInfo;
                        var userInfo = res2.userInfo;
                        that.data.nickName = userInfo.nickName;
                        that.data.avatarUrl = userInfo.avatarUrl;
                        that.data.gender = userInfo.gender; //性别 0：未知、1：男、2：女
                        that.data.province = userInfo.province;
                        that.data.city = userInfo.city;
                        that.data.country = userInfo.country;
                        // wx.setStorageSync("nickName", userInfo.nickName);
                        wx.request({
                          url: that.globalData.publicUrl + 'coreUser/index',
                          method: 'POST',
                          data: {
                            openid: openidUser,
                            crusrWxNickname: that.data.nickName,
                            crusrWxSex: that.data.gender,
                            crusrWxHeadimgurl: that.data.avatarUrl
                            // crusrWxNickname: wx.getStorageSync("nickName"),
                          },
                          header: {
                            'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
                          },
                          success: function (res3) {
                            console.log("登陆成功");
                            that.globalData.userUuid = res3.data.data.crusrUuid;
                            wx.setStorageSync("userUuid", res3.data.data.crusrUuid);
                            console.log(that.globalData.userUuid)
                            //加载框结束
                            wx.hideLoading();
                          }
                        });
                      }
                    });
                  }
                });
              } else {
                console.log('获取用户登录态失败！' + res.errMsg)
              }
            }
          })
        }
      }
    })
  },
  // 全局变量  app.globalData.publicUrl
  globalData: {
    publicUrl: "https://jgj.hzlingdian.com/",
    userInfo: null,
    userUuid: ""
  }
})